﻿using System;
using System.Collections.Generic;
//c#基础
namespace Base
{
    class Programs   //Program
    {
        //static void Main(string[] args)
        //{
            //行注释
            /*
             * 行注释
             */
            //Console.WriteLine("Hello World!");

            ////C#基本数据类型
            //DataType.Show();

            ////C#方法三种参数类型，值，引用，输出
            //int a = 10;
            //Console.WriteLine("实参a=" + a);
            //MethodParameterType.ParameterValue(a);
            //Console.WriteLine("a值传入函数ParameterValue：a=" + a);
            //MethodParameterType.ParameterRef(ref a);
            //Console.WriteLine("a引用传入函数ParameterValue：a=" + a);
            //MethodParameterType.ParameterOut(out a);
            //Console.WriteLine("a输出传入函数ParameterValue：a=" + a);

            ////自定义类型操作符重载
            //Person p1 = new Person("Jack");
            //Person p2 = new Person("Rose");
            ////Person结婚生子 使用 + 号
            //List<Person> list = p1 + p2;
            //foreach(var p in list)
            //{
            //    Console.WriteLine(p.name);
            //}

            ////基础操作符 typeof default
            //BaseOperator.ShowTypeof();
            //BaseOperator.ShowDefault();
            //BaseOperator.ShowNew();
            //BaseOperator.ShowCheck();

            ////一元操作符
            ////因为二进制，取反需要+1，
            //UnaryOperator.Show();
        //}
    }

    //C#基本数据类型
    class DataType
    {
        public static void Show()
        {
            int n1 = 1;
            Console.WriteLine(n1);
            long n2 = 999999999L;
            Console.WriteLine(n2);
            float n3 = 3.2F;
            Console.WriteLine(n3);
            double n4 = 3.44444444;
            Console.WriteLine(n4);
            char c1 = 'c';
            Console.WriteLine(c1);
            string s1 = "string";
            Console.WriteLine(s1);
            bool b1 = true;
            Console.WriteLine(b1);
            bool b2 = false;
            Console.WriteLine(b2);
            var v1 = 3;
            Console.WriteLine("可变类型变量 var=3 由系统转换的类型" + v1.GetType());
            dynamic v2 = 100;
            Console.WriteLine("弱类型变量dynamic v2=" + v2);
            v2 = "abcd";
            Console.WriteLine("弱类型变量dynamic v2=" + v2);
        }
    }

    //C#方法三种参数类型，值，引用，输出
    class MethodParameterType
    {
        //参数类型 值
        public static void ParameterValue(int a)
        {
            a += 1;
        }
        //参数类型 引用
        public static void ParameterRef(ref int a)
        {
            //传入实参的引用（即变量地址），与c++类型引用一样，方法内变量值改变会影响实参
            a += 1;
        }
        //参数类型 输出
        public static void ParameterOut(out int a)
        {
            //输出类型在方法内部必须赋初值,输出类型作用于引用类型相同，一般用于作为额外的返回值使用
            a = 1;
            a += 1;
        }
    }

    //自定义类型操作符重载
    class Person
    {
        public string name;
        public Person(string name)
        {
            this.name = name;
        }

        //重载操作符号+
        public static List<Person> operator +(Person p1, Person p2)
        {
            List<Person> list = new List<Person>();
            for (int i = 0; i < 10; i++)
            {
                Person child = new Person(p1.name + "." + p2.name + "." + "Child" + i);
                list.Add(child);
            }
            return list;
        }
    }
    
    //基础操作符 typeof default new
    class BaseOperator
    {
        //typeof
        public static void ShowTypeof()
        {
            //typeof返回type类型，type可以查看类的名子，方法，属性,事件
            Type t = typeof(int);
            Console.WriteLine("int类型信息");
            Console.WriteLine("int Name:"+t.Name);
            Console.WriteLine("int FullName:" + t.FullName);
            Console.WriteLine("int GetMethods().Length:" + t.GetMethods().Length);
            foreach(var method in t.GetMethods())
            {
                Console.WriteLine(method.Name);
            }
        }

        //default
        public static void ShowDefault()
        {
            //查看系统初始化给类的实例的默认值

            int x = default(int);
            Console.WriteLine("int default value=" + x);

            string s = default(string);
            Console.WriteLine("string default value=null? :" + (s == null));

            Level level = default(Level);
            Console.WriteLine("level default value=" + level);
        }

        enum Level
        {
            Low,//第一个默认序号为0;
            Mid,//之后+1;
            High
        }

        //new
        public static void ShowNew()
        {
            //通过var创建隐式类型应用，接收使用new操作符和初始化器生成的匿名对象。类型都由系统推导。
            var person = new { name = "jack", age = 20 };
            Console.WriteLine(person.name);
            Console.WriteLine(person.age);
            Console.WriteLine(person.GetType());
        }

        //checked unchecked
        public static void ShowCheck()
        {
            uint x = uint.MaxValue;
            Console.WriteLine("x");
            Console.WriteLine(Convert.ToString(x, 2));

            //检验内存溢出 也可以做为关键字作用于代码块，直接检测或者不检查内存溢出
            try
            {
                uint y = unchecked(x + 1);
                Console.WriteLine(y);
                uint z = checked(x + 1);
                Console.WriteLine(z);
            }
            catch (Exception)
            {
                Console.WriteLine("there's overflow");
            }           
        }
    }

    //一元操作符
    class UnaryOperator
    {
        public static void Show(){
            //取反操作符~
            int x = 12345;
            int y = ~x;
            Console.WriteLine("12345 ~取反 = "+y);           
        }
    }



}
